// 对于Windows系统
process.env.FORCE_COLOR = 'true';
//1.导入
const {ipcMain, BrowserWindow} = require("electron")


//3.定义根据地址获取页面信息方法
const getResource = (url) => {
    return new Promise((resolve, reject) => {
        //4.创建一个不可见的窗口
        const win = new BrowserWindow({
            width: 1920, height: 1080, show: false,//隐藏窗口
            webPreferences: {
                offscreen: true,//是否绘制和渲染可视区域外的窗口. 默认值为 false
            }
        })
        win.loadURL(url);
        //5.通过 webContents 获取 渲染以及控制 web 页面
        win.webContents.on('did-finish-load', async () => {
            const contents = win.webContents;
            const title = contents.getTitle();
            const poster = await contents.capturePage({width: 1900, height: 1080, x: 0, y: 0})
            try {
                if (poster.isEmpty()) {
                    resolve({
                        status: 1,
                        title: title,
                        url,
                        poster: poster.toDataURL(),
                    })
                } else {
                    resolve({
                        status: 0,
                        title,
                        url,
                        poster: poster.toDataURL(),
                    })
                }

            } catch (e) {
                reject(e)
            }
        })
    })

}

//2.监听渲染进程发送的 on-url-event 事件
ipcMain.handle('on-url-event', async (event, args) => {
    return await getResource(args);
})


